<template>
  <div class="box">
    <!-- 搜索框 -->
    <div class="search">
      <div class="insearch">
        <div class="searchtop">
          <span>筛选搜索</span>
          <span>
            <el-button plain>重置</el-button>
            <el-button type="primary">搜索查询</el-button>
          </span>
        </div>
        <div class="searchbot">
          账户/名称：<input type="text" placeholder="请输入账号、密码" />
        </div>
      </div>
    </div>
    <!-- 添加框 -->
    <div class="add">
      <span>数据列表</span>
      <el-button type="primary" @click="add">添加</el-button>
    </div>
    <!-- 内容列表 -->
    <div class="content">
      <!-- 用户列表 -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        :cell-style="{ 'text-align': 'center' }"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="nickName" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column label="创建时间">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="最后登录">
          <template #default="{ row }">
            {{ formatDate(row.loginTime) }}
          </template>
        </el-table-column>

        <el-table-column label=" 是否启用">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              :active-value="1"
              :inactive-value="0"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button @click="allocRoleInfo(row)">分配角色</el-button>
            <el-button @click="editAdminInfo(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:currentPage="pager.currentPage"
        v-model:page-size="pager.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        :small="pager.small"
        :disabled="pager.disabled"
        :background="pager.background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="400"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        class="fenye"
      />

      <!-- 用户编辑弹窗 -->
      <EditAdmin
        :visible="editAdminvisible"
        @close="closeAdminInfo"
        :form-data="editAdminData"
      ></EditAdmin>

      <AllocRoles
        :visible="allocRolesVisible"
        @close="closeAllocRole"
        :admin-id="adminId"
      />
    </div>
  </div>
</template>

<!-- /admin/list -->

<script lang="ts" setup>
import { ref, reactive } from "vue";
import dayjs from "dayjs";

import { umsAdmin } from "../../../request/http";

// 引入用户弹窗
import EditAdmin from "./components/EditAdmin.vue";
// 修改权限
import AllocRoles from "./components/AllocRoles.vue";

// 存储用户数据
let tableData = ref([] as {}[]);

// 分页数据
let pager = reactive({
  currentPage: 1,
  pageSize: 5,
  small: true,
  disabled: false,
  background: true,
  total: 0,
});

// 分配角色弹框操作
const allocRolesVisible = ref(false);
const adminId = ref();
function allocRoleInfo(row: AdminResDataObj) {
  allocRolesVisible.value = true;
  adminId.value = row.id;
}
function closeAllocRole() {
  allocRolesVisible.value = false;
}

const editAdminvisible = ref(false);
const editAdminData = ref({} as AdminResDataObj);

// 角色变量
let showRole = ref(false);

// 角色事件
function role() {
  showRole.value = true;
}

// 添加用户信息、注册用户
function add() {
  editAdminvisible.value = true;
  editAdminData.value = {} as AdminResDataObj;
}

//打开关闭窗口
function editAdminInfo(row: AdminResDataObj) {
  editAdminvisible.value = true;
  editAdminData.value = row;
}

// 关闭用户信息弹窗
function closeAdminInfo(r?: "reload") {
  editAdminvisible.value = false;
  if (r === "reload") {
    // 更新表格
    fetchData();
  }
}

// 分页事件
function handleSizeChange() {
  fetchData(pager.currentPage, pager.pageSize);
}
function handleCurrentChange() {
  fetchData(pager.currentPage, pager.pageSize);
}

// 请求用户列表
async function fetchData(pageNum: number = 1, pageSize: number = 10) {
  const adminListRes = await umsAdmin.getAdminList({ pageNum, pageSize });
  if (adminListRes.code === 200) {
    tableData.value = adminListRes.data.list;
    pager.total = adminListRes.data.total;
  }
}
fetchData(pager.currentPage, pager.pageSize);

// 时间格式化   注意需要安装dayjs
function formatDate(time?: string) {
  if (!time) return "";
  return dayjs(time).format("YYYY-MM-DD  HH-mm-ss");
}
const value1 = ref(true);
</script>

<style lang="less" scoped>
.box {
  margin: 20px;
  .search {
    border: 1px solid #eee;
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
    .insearch {
      margin: 20px;
      height: 92px;
      // background-color: orange;
      .searchtop {
        display: flex;
        justify-content: space-between;
      }
      .searchbot {
        padding: 0 50px;
        input[type="text"] {
          margin-top: 20px;
          padding: 10px;
          height: 10px;
          inset: none;
          outline: none;
          // border: 1px solid #409eff;
          border: 1px solid #eee;
          border-radius: 5px;
        }
      }
    }
  }
  .add {
    box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.2);
    margin-top: 20px;
    padding: 20px;
    height: 40px;
    // background-color: #409eff;
    line-height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid #eee;
  }
  .content {
    margin-top: 20px;
    .dowork {
      color: #0f7aec;
      span {
        padding: 10px;
        cursor: pointer;
      }
    }
  }
  .fenye {
    float: right;
    padding: 20px;
  }
}
</style>
